import React, { Component, createRef } from 'react'
import { Card, Form, Input, Button, Row, Col, message } from 'antd';
import {  getAllGoods, getTypesList, updateOne } from '../../api/productsList';



const success = (msg) => {
    message.success(msg);
};
export default class AddList extends Component {
    state = {
        options: [],
        updateInfo: {},
        type: ''
    }
    formRef = createRef()
    componentDidMount() {
        this.getTypesList()
    }
    // 获取列表
    getTypesList = async () => {
        console.log(this.props.location.query.id);
        const getAll = await getAllGoods()
        if (getAll.code) {
            const getGood = getAll.data.filter(item => item._id === this.props.location.query.id)[0]
            console.log(getGood);
            this.formRef.current.setFieldsValue({
                updateInfo: getGood
            })
        }
        //获取分类
        const res = await getTypesList()
        console.log(res);
        if (res.code) {
            this.setState({
                options: res.data
            })
        }
    }
    // 获取type的id值
    onChange = (value, selectedOptions) => {
        this.setState({
            type: selectedOptions[1].id
        })
    }

    render() {
        const { updateInfo } = this.state
        // 点击提交时候触发
        const onFinish =async (values) => {
          console.log('values',values.updateInfo);

          const res= await updateOne({...values.updateInfo,id:values.updateInfo._id,type:values.updateInfo.type._id})
          console.log('触发的结果',res);
        if (res.code) {
            message.success(res.msg);
            this.props.history.push('/products/list')
        } else {
            message.warning(res.msg);
        }
        };

        const onFinishFailed = (errorInfo) => {
            console.log('Failed:', errorInfo);
        };

        return (
            <Card title="修改商品" extra={<a href="#">More</a>}>
                <Row>
                    <Col span={10}>
                        <Form
                            ref={this.formRef}
                            name="basic"
                            labelCol={{
                                span: 8,
                            }}
                            wrapperCol={{
                                span: 16,
                            }}
                            initialValues={
                                updateInfo
                            }
                            onFinish={onFinish}
                            onFinishFailed={onFinishFailed}
                            autoComplete="off"
                        >
                            <Form.Item
                                label="商品编号"
                                name={['updateInfo', '_id']}
                                rules={[
                                    {
                                        required: true,
                                        message: 'Please input your name!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                            <Form.Item
                                label="价格"
                                name={['updateInfo', 'price']}
                                rules={[
                                    {
                                        required: true,
                                        message: 'Please input your title!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                            <Form.Item
                                label="分类"
                                name={['updateInfo', 'type','_id']}
                                rules={[
                                    {
                                        required: true,
                                        message: 'Please input your type!',
                                    },
                                ]}
                            >
                                <Input />
                            </Form.Item>
                            <Form.Item
                                wrapperCol={{
                                    offset: 12,
                                    span: 12,
                                }}
                            >
                                <Button type="primary" htmlType="submit" >
                                    提交
                                </Button>
                            </Form.Item>
                        </Form>
                    </Col>
                </Row>

            </Card>
        )
    }
}
